<template>
	<view class="zai-box">
		<image src="../../../static/logo.png" mode='aspectFit' class="zai-logo"></image>
		<view class="zai-form">
			<input class="zai-input"  placeholder="请输入帐号" v-model="username"/>
			<input class="zai-input"  placeholder="请输入手机号码" v-model="mobile"/>
			<view class="zai-input-btn">
				<input class="zai-input"  placeholder="验证码" v-model="code"/>
				<view class="zai-checking" @click="checking" v-if="state===false">获取验证码</view>
				<view class="zai-checking zai-time" v-if="state===true">倒计时{{ currentTime }}s</view>
			</view>
			<input class="zai-input"  password placeholder="请输入密码" v-model="password"/>
			<button class="zai-btn" @click="register()">立即注册</button>
			<view class="zai-label">已有账号，点此去登录.</view>
			<!-- <navigator url="../pages/Login/Loginfour" open-type='navigateBack' hover-class="none" class="zai-label">已有账号，点此去登录.</navigator> -->
		</view>
	</view>
</template>

<script>
	import {register} from '@/api/mine.js'
	export default {
		data() {
			return {
				state: false,		//是否开启倒计时
				totalTime: 60,		//总时间，单位秒
				recordingTime: 0, 	//记录时间变量
				currentTime: 0, 	//显示时间变量
				username:'',//帐号
				mobile:'',//手机号码
				password:'',//密码
				code:'',//验证码
			}
		},
		onLoad() {
	
		},
		methods: {
			register(){
				// if(!this.username){
				// 	uni.showToast({
				// 		title:'请输入帐号'
				// 	})
				// }
				// if(!this.password){
				// 	uni.showToast({
				// 		title:'请输入密码'
				// 	})
				// }
				// if(!this.mobile){
				// 	uni.showToast({
				// 		title:'请输入手机号码'
				// 	})
				// }
				register({
					username:this.username,
					mobile:this.mobile,
					verify:this.code,
					password:this.password
				}).then(res=>{
					if(res.status==200){
						console.log("register",res)
						uni.navigateTo({
							url:'../Login/Loginfour'
						})
					}
				
				}).catch(e => {
					console.log(e)
				})
				
			},
			checking() {
				//把显示时间设为总时间
				this.currentTime = this.totalTime;
				//开始倒计时
				this.state = true;
				//执行倒计时
				this.checkingTime();
			},
			checkingTime(){
				let that = this;
				//判断是否开启
				if(this.state){
					//判断显示时间是否已到0，判断记录时间是否已到总时间
					if(this.currentTime > 0 && this.recordingTime <= this.totalTime){
						//记录时间增加 1
						this.recordingTime = this.recordingTime + 1;
						//显示时间，用总时间 - 记录时间
						this.currentTime = this.totalTime - this.recordingTime;
						//1秒钟后，再次执行本方法
						setTimeout(() => { 	
							//定时器内，this指向外部，找不到vue的方法，所以，需要用that变量。
							that.checkingTime();
						}, 1000)
					}else{
						//时间已完成，还原相关变量
						this.state = false;		//关闭倒计时
						this.recordingTime = 0;	//记录时间为0
						this.currentTime = this.totalTime;	//显示时间为总时间
					}
				}else{
					//倒计时未开启，初始化默认变量
					this.state = false;
					this.recordingTime = 0;
					this.currentTime = this.totalTime;
				}
			}
		}
	}
</script>

<style>
	.zai-box{
		padding: 0 100rpx;
		position: relative;
	}
	.zai-logo{
		margin-top: 50rpx;
		width: 100%;
		height: 210rpx;
	}
	.zai-form{
		margin-top: 120rpx;
	}
	.zai-input{
		background: #e2f5fc;
		margin-top: 30rpx;
		border-radius: 100rpx;
		padding: 20rpx 40rpx;
		font-size: 36rpx;
	}
	.input-placeholder, .zai-input{
		color: #94afce;
	}
	.zai-label{
		padding: 60rpx 0;
		text-align: center;
		font-size: 30rpx;
		color: #a7b6d0;
	}
	.zai-btn{
		background: #00C049;;
		color: #fff;
		border: 0;
		border-radius: 100rpx;
		font-size: 36rpx;
		margin-top: 60rpx;
	}
	.zai-btn:after{
		border: 0;
	}
	
	/*验证码输入框*/
	.zai-input-btn{
		position: relative;
	}
	.zai-input-btn .zai-input{
		padding-right: 260rpx;
	}
	.zai-checking{
		position: absolute;
		right: 0;
		top: 0;
		background: #00C049;;
		color: #fff;
		border: 0;
		border-radius: 110rpx;
		font-size: 36rpx;
		margin-left: auto;
		margin-right: auto;
		padding-left: 28rpx;
		padding-right: 28rpx;
		box-sizing: border-box;
		text-align: center;
		text-decoration: none;
		line-height: 2.55555556;
		-webkit-tap-highlight-color: transparent;
		overflow: hidden;
		padding-top: 2rpx;
		padding-bottom: 2rpx;
	}
	.zai-checking.zai-time{
		background: #a7b6d0;
	}
	
	/*按钮点击效果*/
	.zai-btn.button-hover{
		transform: translate(1rpx, 1rpx);
	}
</style>
